package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@SuppressWarnings("all")
public class ListController {
    @Autowired
    private ListFeignClient listFeignClient;

    /**
     *list.html:这个是当浏览器有这个请求时就会执行这个controller,
     * 然后SearchParam类封装所有携带的参数,Model是往session中存数据
     * 方便页面通过th:的方式获取返回的结果
     * listFeignClient这个是注入的远程调用的接口
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){
        Result<Map> list = listFeignClient.list(searchParam);
        model.addAllAttributes(list.getData());
        //路径拼接
        String urlParam=makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);

        //品牌的面包屑
        String trademark = searchParam.getTrademark();
        String trademarkParam=getTrademarkParam(trademark);
        model.addAttribute("trademarkParam",trademarkParam);

        //平台属性面包屑
        String[] props = searchParam.getProps();
        List<Map<String,String>> propsParamList=getPropsParamList(props);
        model.addAttribute("propsParamList",propsParamList);

        //排序规则封装
        String order = searchParam.getOrder();
        Map<String,String> orderMap=getOrderMap(order);
        model.addAttribute("orderMap",orderMap);

        return "list/index";
    }

    /**
     * 排序规则封装
     * @param order
     * @return
     */
    private Map<String, String> getOrderMap(String order) {
        Map<String, String> orderMap=new HashMap<>();
        if(!StringUtils.isEmpty(order)){
            String[] split = StringUtils.split(order, ":");
            if(null!=split && split.length==2){
                orderMap.put("type",split[0]);
                orderMap.put("sort",split[1]);
            }
            return orderMap;
        }else{
            orderMap.put("type","1");
            orderMap.put("sort","desc");
            return orderMap;
        }
    }

    /**
     * 平台属性面包屑
     * @param props
     * @return
     */
    private List<Map<String, String>> getPropsParamList(String[] props) {
        List<Map<String,String>> propsParamList = new ArrayList<>();

        if(props != null && props.length > 0){
            for (String prop : props) {
                Map<String, String> propMap = new HashMap<>();
                String[] split = prop.split(":");
                if(split.length==3){
                    propMap.put("attrId",split[0]);
                    propMap.put("attrValue",split[1]);
                    propMap.put("attrName",split[2]);
                }
                propsParamList.add(propMap);
            }
        }
        return propsParamList;
    }

    /**
     * 品牌的面包屑
     * @param searchParam
     * @return
     */
    private String getTrademarkParam(String trademark) {
        if(trademark!=null){
            String[] split = trademark.split(":");
            if(split !=null && split.length==2){
                return "品牌:"+split[1];
            }
        }
        return null;
    }

    /**
     * 路径拼接
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuffer urlParam=new StringBuffer();

        //先判断有没有关键字搜索
        if(searchParam.getKeyword()!=null){
            //把关键字信息拼接到路径中
            urlParam.append("keyword=").append(searchParam.getKeyword());
        }
        //判断一级分类id
        if(searchParam.getCategory1Id()!=null){
            //把一级分类id拼接到路径中
            urlParam.append("category1Id=").append(searchParam.getCategory1Id());
        }
        //判断二级分类id
        if(searchParam.getCategory2Id()!=null){
            //把二级分类id拼接到路径中
            urlParam.append("category2Id=").append(searchParam.getCategory2Id());
        }
        //判断三级分类id
        if(searchParam.getCategory3Id()!=null){
            //把三级分类id拼接到路径中
            urlParam.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //根据业务逻辑,肯定有关键字或者分类id否则都无法进入list页面
        if(urlParam.length()>0){
            //判断品牌
            if(!StringUtils.isEmpty(searchParam.getTrademark())){
                //把品牌信息拼接到路径中
                urlParam.append("&trademark=").append(searchParam.getTrademark());
            }

            //判断平台属性
            if(null != searchParam.getProps() && searchParam.getProps().length>0){
                for (String prop : searchParam.getProps()) {
                    urlParam.append("&props=").append(prop);
                }
            }
        }

        return "list.html?"+urlParam.toString();
    }
}
